#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 10 22:54:17 2018
@project: 天池比赛-A股主板上市公司公告信息抽取
@group: MZH_314
@author: LHQ
"""
import array

import numpy as np
from scipy.sparse import csr_matrix


class BooleanVectorizer:
    """
    布尔值Vectorizer(模仿sklearn里的CountVectorizer),
    构造特征词的特征列表，出现特征词则对应的索引位置置1，否则置0
    """
    def __init__(self):
        self._feature_words = []
        self.feature_index = dict()
        self.index_feature = dict()
        
    @classmethod
    def of_feature_words(cls, words:list):
        """根据特征词列表生成vectorizer
        """
        vec = cls()
        vec.fit_transform(words)
        return vec
    
    @property
    def feature_words(self):
        return self._feature_words
    
    def get_feature_words(self):
        """获取特征词
        """
        return self._feature_words
    
    def fit_transform(self, words:list):
        """在转换词语为特征举证之前，先告诉vectorizer哪些词作为特征词
        """
        words = list(set(words))  # 特征词去重
        self._feature_words = words
        for i, w in enumerate(words):
            self.feature_index[w] = i
            self.index_feature[i] = w
        
    def _transform(self, words:list):
        """将外部的词语们转换成特征数组
        """
        feature = [0 for _ in self._feature_words]
        for w in words:
            index = self.feature_index.get(w, None)
            if index is None:
                continue
            feature[index] = 1
        return feature
    
    def transform(self, WORDS:list):
        """将外部的一系列词语转换成特征矩阵
        """
        features = []
        for words in WORDS:
            feature = self._transform(words)
            features.append(feature)
        return np.array(features)
    
    def transform2(self, WORDS:list):
        """transforms的稀疏矩阵实现，暂时还存在一些问题
        """
        indptr = array.array(str('i'))
        indices = []
        values = array.array(str('i'))
        indptr.append(0)
        for words in WORDS:
            feature = self._transform(words)
            f_i = [(f, i) for i, f in enumerate(feature)]
            f_i_nz = [x for x in f_i if x[0] != 0]
            try:
                index, value = list(zip(*f_i_nz))
            except ValueError:
                index, value = [0], [0]
            indices.extend(index)
            values.extend(value)
            indptr.append(len(value))
        features = csr_matrix((values, indices, indptr), 
                              shape=(len(indptr)-1, len(self._feature_words)))
        return features
